﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;



namespace MonitorProcessResponding
{
    class Program
    {
        static void Main(string[] args)
        {


            try
            {
                //char[] a = { ';'};
                string processname = args[0];
                int looptime = int.Parse(args[1]);
                int waittime = int.Parse(args[2]);
                bool loop = false;
                try
                {
                    if (args[3].ToLower() == "loop")
                    {
                        loop = true;
                    }
                }
                catch
                {

                }
                            monitor(processname, looptime, waittime, loop);//, capturescreen, copylog, logfilepath,of);


            }
            catch
            {
                Console.WriteLine("MonitorProcessResponding <processname> <looptime> <waittime> [<loop [<capturescreen> <copylog> <logfilepath> <outlog>]>]"); 
            }




        }

        static void monitor(string pn,int lt,int wt,bool lp)//,bool cs,bool cl,string lf,string outfile)
        {
            

            if (lp)
            {
                while (true)
                {
                    try
                    {
                        Process[] pp = Process.GetProcessesByName(pn);
                        foreach (Process p in pp)
                        {
                            if (p.Responding == false)
                            {
                                Console.WriteLine("{0}:{1} is not responding. {1} will be killed after {2} seconds if it is not responding.", DateTime.Now, p.ProcessName, wt);
                                Thread.Sleep(wt * 1000);
                                if (p.Responding == false)
                                {

                                    string strtime = DateTime.Now.ToFileTime().ToString();
             
                                    p.Kill();
                                    Console.WriteLine("{0}:{1} is killed.", DateTime.Now, p.ProcessName);
                                  
                                }
                                else
                                {
                                    Console.WriteLine("{0}:{1} is not responding. {1} has been responding.", DateTime.Now, p.ProcessName);
                                }
                            }


                        }
                    }
                    catch
                    {
                    }
                    Thread.Sleep(lt * 1000);
                }

            }
            else
            {
                bool flag = true;
                while (flag)
                {
                    try
                    {
                        Process[] pp = Process.GetProcessesByName(pn);
                        foreach (Process p in pp)
                        {
                            if (p.Responding == false)
                            {
                                Console.WriteLine("{0}:{1} is not responding. {1} will be killed after {2} seconds if it is not responding.", DateTime.Now, p.ProcessName, wt);
                                Thread.Sleep(wt * 1000);
                                if (p.Responding == false)
                                {

                                    p.Kill();
                                    Console.WriteLine("{0}:{1} is killed.", DateTime.Now, p.ProcessName);
                                    flag = false;
                                }
                            }


                        }
                    }
                    catch
                    {
                    }
                    Thread.Sleep(lt * 1000);
                }
            }

        }

       
        



    }
}
